<?php

namespace app\api\model;

use think\Model;

class TrainList extends Model
{
    protected $autoWriteTimestamp = true;

    protected $pk = 'train_id';

    public static function getTodayInfoByUid($userId)
    {
        return self::where('user_id', '=', $userId)
            ->where('begin_time', '<=', strtotime(date('Y-m-d')))
            ->where('end_time', '>=', strtotime(date('Y-m-d')))
            ->find();
    }

    public static function getInfoByTrainId($trainId)
    {
        return self::where('train_id', '=', $trainId)->find();
    }

    public static function getHistoryList($userId, $pageNo, $pageSize)
    {
        return self::where('user_id', '=', $userId)
            ->where('train_time', '<', strtotime(date('Y-m-d')))
            ->page($pageNo, $pageSize)
            ->select()
            ->toArray();
    }

    public static function countHistoryList($userId)
    {
        return self::where('user_id', '=', $userId)
            ->where('train_time', '<', strtotime(date('Y-m-d')))
            ->count();
    }

    public static function checkTrainFinish($trainId)
    {
        $exist = TrainChild::where('train_id', '=', $trainId)
            ->where('train_status', 0)
            ->find();
        if (!$exist) self::update(['train_id' => $trainId, 'train_status' => 1]);
        return true;
    }
}